﻿using System;
using System.Web.UI;
using CL.Tools.Common;
using CL.Enum.Common.Union;
using CL.Game.BLL;
using CL.Enum.Common;
using CL.Business.BLL;

namespace CL.UnionWeb
{
    public partial class Login : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ("POST" == Request.RequestType)
                {
                    string AccountCode = Request["txtLoginName"];
                    string AccountPwd = Request["txtLoginPwd"];
                    if (string.IsNullOrEmpty(AccountCode) || string.IsNullOrEmpty(AccountPwd))
                    {
                        ReturnMsgToView("请输入推广联盟账号和密码");
                    }

                    var Entity = new AccountBLL().QueryEntity(AccountCode);
                    if (Entity == null)
                    {
                        ReturnMsgToView("账号不存在");
                    }

                    AccountPwd = Utils.MD5(AccountPwd);
                    if (Entity.AccountPwd != AccountPwd)
                    {
                        new RecordBLL().InsertEntity(new Business.Entity.RecordEntity()
                        {
                            AccountID = Entity.AccountID,
                            RecordTime = DateTime.Now,
                            RecordRemark = string.Format("密码错误"),
                            RecordType = 1
                        });
                        ReturnMsgToView("帐号或密码错误");
                    }
                    else
                    {
                        new RecordBLL().InsertEntity(new Business.Entity.RecordEntity()
                        {
                            AccountID = Entity.AccountID,
                            RecordTime = DateTime.Now,
                            RecordRemark = string.Format("代理推广登录"),
                            RecordType = 1
                        });
                        Session[CLKeys.SESSION_ADMIN_INFO] = Entity;
                        Session.Timeout = 30;
                        //  Utils.WriteCookie("UserName", "CLAdmin", Entity.AccountCode);
                        //  Utils.WriteCookie("UserPwd", "CLAdmin", Entity.AccountPwd);
                        if ((int)AccountStatusEnum.toBeAudited == Entity.AccountStatus ||
                            (int)AccountStatusEnum.certification == Entity.AccountStatus ||
                            (int)AccountStatusEnum.noPass == Entity.AccountStatus)
                            ReturnMsgToView("ok_MyAccount.aspx");
                        else
                            ReturnMsgToView("ok_Index.aspx");
                    }
                }
        }

        protected void ReturnMsgToView(string msg)
        {
            Response.Write(msg);
            Response.End();
        }
       
    }
}